package ru.spb.osll.myfit.client.widget.filter;


import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.*;

import ru.spb.osll.myfit.common.filter.MatchFilter;
import ru.spb.osll.myfit.common.filter.PageResultFilter;

import static ru.spb.osll.myfit.client.widget.IStyleConstants.*;


/**
 * User: Alexander Opeykin
 * Date: 4/23/12
 */
public class MatchFilterWidget extends FilterWidget {
  private final Button deleteButton = new Button();
  private FilterInitializer initializer;

  public MatchFilterWidget(MatchFilter filter) {
    HorizontalPanel panel = new HorizontalPanel();
    initWidget(panel);

    TextBox textBox = new TextBox();
    textBox.setText(filter.getValue());
    textBox.addStyleName(FILTER_VALUE_STYLE);

    deleteButton.addStyleName(FILTER_DEL_BUTTON_STYLE);

    Label nameLabel = new Label(filter.getKey());
    nameLabel.addStyleName(FILTER_NAME_STYLE);
    nameLabel.addStyleName(FILTER_NAME_LABEL_STYLE);

    panel.addStyleName(FILTER_PANEL_STYLE);
    panel.add(nameLabel);
    panel.add(textBox);
    panel.add(deleteButton);

    initializer = new TextBoxFilterInitializer(textBox, filter);
  }

  @Override
  public PageResultFilter getFilter() {
    return initializer.getInitializedFilter();
  }

  @Override
  public void addDeleteHandler(ClickHandler handler) {
    deleteButton.addClickHandler(handler);
  }
}
